package com.zky.gulimall.seckill.controller;

import com.zky.common.utils.R;
import com.zky.gulimall.seckill.service.SecKillService;
import com.zky.gulimall.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description 秒杀controller
 * @Author 张开源
 * @Date 2022/12/27 17:17
 * @Version 1.0
 **/
@Controller
public class SecKillController {
    @Autowired
    private SecKillService secKillService;

    /**
     * 当前时间可以参与的秒杀商品信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSecKillSkus")
    public R getCurrentSecKillSkus() {
        List<SecKillSkuRedisTo> redisTos = secKillService.getCurrentSecKillSkus();
        return R.ok().put("data", redisTos);
    }

    /**
     * 当前时间可以参与的秒杀商品信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/secKill/{skuId}")
    public R getSkuSecKillInfo(@PathVariable("skuId") Long skuId) {
        SecKillSkuRedisTo redisTo = secKillService.getSkuSecKillInfo(skuId);
        return R.ok().put("data", redisTo);
    }

    /**
     * 秒杀
     *
     * @return
     */
    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num
            , Model model) {
        String orderSn = secKillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
